class MetricManager:
    """Keep track of metrics and best-value logic."""

    def __init__(self, monitor: str = 'val_loss', mode: str = 'min'):
        self.metrics = {}
        self.monitor = monitor
        assert mode in ('min', 'max')
        self.mode = mode
        self.best = None

    def update(self, **kwargs):
        for k, v in kwargs.items():
            self.metrics.setdefault(k, [])
            self.metrics[k].append(float(v))

    def is_best(self) -> bool:
        if self.monitor not in self.metrics:
            return False
        value = self.metrics[self.monitor][-1]
        if self.best is None:
            self.best = value
            return True
        if self.mode == 'min' and value < self.best:
            self.best = value
            return True
        if self.mode == 'max' and value > self.best:
            self.best = value
            return True
        return False